<?php

/**
 * \class event_details_viewer
 * \brief This class queries the database and get details for selected event
 * and produces html content regarding it.
 * \author Salman Noor
 */
class event_details_viewer {

    /**
     * \brief Object to be able use the html_builder class functionality.
     */
    private $html_buillder;
    /**
     * \brief Object that does CRUD functions on the database.
     */
    private $dbtables_handler;

    /**
     * \brief Contructor to initialise mdata members of the class event_details_viewer
     */
    public function __construct() {
        $this->html_buillder = new html_elements_builder();
        $this->dbtables_handler = new database_tables_handler();
    }

    /**
     * \brief Builds html content for action button.
     * \return htlm content for action button
     */
    private function buildEventActionButtons() {
        $eventActions = $this->html_buillder->buildButton("viewEvent", "View Details for Event", "viewDetails");
        $eventActions .= $this->html_buillder->buildButton("deleteEvent", "Delete Event", "deleteSelectedEvent");
        return $eventActions;
    }

    /**
     * \brief Lists all available activities.
     * \return html content
     */
    private function listAllActivitiesbyName() {
        $listStyle = "<style>
	#feedback { font-size: 1.4em; }
	#selectableCurrentEventList .ui-selecting { background: #FECA40; }
	#selectableCurrentEventList .ui-selected { background: #F39814; color: white; }
	#selectableCurrentEventList { list-style-type: none; margin: 0; padding: 0; width: 60%; }
	#selectableCurrentEventList li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
	</style>";

        $eventList = $listStyle;
        $eventList .= "<p id='alterationOptions'>
         <b> Selected Event:</b>  <span id='select-result'>none</span><br>
<span>" . $this->buildEventActionButtons() . "
            </span>
   
</p>";

        $eventList .="<ol id='selectableCurrentEventList'>";
        if (isset($_SESSION['user_id'])) {
            $currentEventsArray = ($this->dbtables_handler->getAllEvents($_SESSION['user_id']));
            foreach ($currentEventsArray as $thisCurrentEventParameter) {
                $eventList .="<li title=$thisCurrentEventParameter[event_id] class='ui-widget-content'>$thisCurrentEventParameter[event_name]</li>";
            }

            $eventList .="</ol>";
            return $eventList;
        } else {
            return "Error. You have entered this page illegaly. Session variables are not set.";
        }
    }

    /**
     * \brief Builds html content for Dialogue Box.
     * \return html dialog box content
     */
    private function buildDetailedContentForEventDialog() {
        return $this->html_buillder->buildDialogueBoxWithContent("dialog-singleEventDetails", "Details For Event", "detailsForEventContainer", "");
    }

    /**
     * \brief This function builds grid for selecting event types.
     * \retun html content for event type grid
     */
    private function buildGridOfEventTypes($eventTypeString) {
        $eventTypeArray = explode(",", $eventTypeString);
        $gridStyle = "<style>
	#selectableEventTypeGrid .ui-selecting { background: #FECA40; }
	#selectableEventTypeGrid .ui-selected { background: #F39814; color: white; }
	#selectableEventTypeGrid { list-style-type: none; margin: 0; padding: 0; }
	#selectableEventTypeGrid li { margin: 3px; padding: 1px; float: left; width: 200px; height: 25px; font-size: 1em; text-align: center; }
	</style>";

//print_r($eventTypeArray);

        $gridOfEventTypes = $gridStyle;


        $gridOfEventTypes .= "<ol id='selectableEventTypeGrid'>";
        foreach ($eventTypeArray as $key => $value) {
            // echo "Key: $key; Value: $value<br />\n";
            $gridOfEventTypes .="<li title=$key class='ui-state-default'>$value</li>";
        }


        $gridOfEventTypes .= "</ol><br><br>";
        return $gridOfEventTypes;
    }

    /**
     * \brief Convert time from GMT to normal time, i.e. from 14h00 to 2:00 pm.
     * \param hours An interger number.
     * \param minutes An interger number.
     * \return Converted time 
     */
    private function convertToReadableTime($hours, $minutes) {
        if ($hours < 12) {
            $time = "AM";
        } else {
            $time = "PM";
        }
        if ($hours == 0) {
            $hours = 12;
        }
        if ($hours > 12) {
            $hours = $hours - 12;
        }
        if (strlen($minutes) == 1) {
            $minutes = "0" . $minutes;
        }
        return $hours . ":" . $minutes . " " . $time;
    }

    /**
     * \brief Builds accordion for activities.
     * \param EventID is integer.
     * \return Returns html content for the activity list based on the EventId.
     */
    private function buildActvityDetailList($eventId) {
        $singleEventActvities = $this->dbtables_handler->getAllActivities($eventId);

        $accordionStyle = "<style>
.ui-accordion-content-active{ overflow:visible !important; }
        </style>";

        $activityList = "<h3>List of Activities</h3>";

        $activityList .= "<div id='actvityDetailsAccordion'>";
        foreach ($singleEventActvities as$thisEventActvity) {

            $activityList .= "<h3><a href='#'>$thisEventActvity[optime_act_name]</a></h3>";
            $activityList .="<div>";
            $activityList .="<p><b>Time Span</b><br>from " . $this->convertToReadableTime($thisEventActvity[optime_openhr], $thisEventActvity[optime_openmin]) . "  to  " . $this->convertToReadableTime($thisEventActvity[optime_closehr], $thisEventActvity[optime_closemin]) . "</p>";
            $activityList .="<p><b>Actvity Desciption</b><br> $thisEventActvity[optime_act_descr]</p>";
            $activityList .="<p><b>Activities Requirements</b><br> $thisEventActvity[optime_act_reqs]</p>";
            $activityList .="</div>";
        }
        $activityList .= "</div>";
        return $activityList;
    }

    /**
     * \brief Builds html content for event .
     * \param EventID is integer.
     * \return Returns html content for the event based on the EventId.
     */
    private function buildDetailedContentForEvent($eventId) {
        $singleEventDetails = $this->dbtables_handler->getEvent($eventId);

        $eventImageDetails = $this->dbtables_handler->getImageDetails($singleEventDetails[0]['event_logo']);
        $detailedContent = "<div id='styledEventDetailsContainer' class='ui-widget-content ui-corner-all'style='border:1px solid #CCCCCC;padding:20px 20px 20px 20px;'> ";
        $detailedContent .= "<h2>" . $singleEventDetails[0]['event_name'] . "</h2>";
        if ($singleEventDetails[0]['event_logo'] == null || $singleEventDetails[0]['event_logo'] == 0) {
            $detailedContent .= "<div id='imageContainer' class='ui-widget-content ui-corner-all'style='border:1px solid #CCCCCC;padding:20px 20px 20px 20px; width:700px;height:350px;overflow:auto;'> ";
            $detailedContent .= "<h3>Image For Event</h3>";
            $detailedContent .= "<img src='images/imageNotFound.png' width='350' height='350'/>";
            $detailedContent .= "</div>";
        } else {
            $detailedContent .= "<div id='imageContainer' class='ui-widget-content ui-corner-all'style='border:1px solid #CCCCCC;padding:20px 20px 20px 20px; width:700px;height:350px;overflow:auto;'> ";
            $detailedContent .= "<h3>Image For Event</h3>";
            $detailedContent .= "<b>Image Name : </b>" . $eventImageDetails['image_username'] . "<br>";
            $detailedContent .= "<b>Image Caption : </b>" . $eventImageDetails['image_caption'] . "<br>";
            $detailedContent .= "<img src='images/" . $singleEventDetails[0]['event_logo'] . ".jpg'/>";
            $detailedContent .= "</div>";
        }

        $detailedContent .= $this->html_buillder->buildTextArea("eventAddressDisplayer", $singleEventDetails[0]['event_address'], "addressDisplayerContainer", TRUE, '<b>Event Address</b>', 'top', 4, 67) . "<br><br>";
        $detailedContent .= $this->html_buillder->buildTextArea("eventDescriptionDisplayer", $singleEventDetails[0]['event_discr'], "decriptionDisplayerContainer", TRUE, '<b>Event Description</b>', 'top', 4, 67) . "<br><br>";
        $detailedContent .= $this->html_buillder->buildTextInput('eventWebsiteDisplayer', $singleEventDetails[0]['event_webaddress'], 'websiteDisplayer', TRUE, "<b>Event Website Address</b>", 'top', '68') . "<br><br>";
        $detailedContent .= "<b>Event Types</b>";
        $detailedContent .= $this->buildGridOfEventTypes($singleEventDetails[0]['event_type']);
        $detailedContent .= $this->html_buillder->buildTextInput('eventLatDisplayer', $singleEventDetails[0]['event_loc_lat'], 'locLatDisplayer', TRUE, "<b>Event Latitude Coordinate (Degrees)</b>", 'top', '68') . "<br><br>";
        $detailedContent .= $this->html_buillder->buildTextInput('eventLongDisplayer', $singleEventDetails[0]['event_loc_long'], 'locLongDisplayer', TRUE, "<b>Event Longitude Coordinate (Degrees)</b>", 'top', '68') . "<br><br>";
        $detailedContent .= $this->buildActvityDetailList($eventId);
        $detailedContent .= "</div>";
        return $detailedContent;
    }

    /**
     * \brief Builds html content for confirming the deletion of the event.
     * \return Returns html content confirmation modal window.
     */
    private function buildDeleteEventConfirmModalWindow() {

        $message = 'The selected event will be deleted and cannot be recovered. Are you sure?';
        $content = "<div style =' border: 1px solid transparent; padding: 7px' class='errorMessageDiv'> </div>
        <p>
                   <span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'>
                   </span>
                   $message
                   </p>";
        return $this->html_buillder->buildDialogueBoxWithContent('dialog-deleteConfirm', 'Confirm Delete Event', 'confirmDeleteEventContainer', $content);
        // return $this->html_buillder->buildSimpleDialogueBox('dialog-deleteConfirm', 'Confirm Delete Event', 'The selected event will be deleted and cannot be recovered. Are you sure?', 'ui-icon-alert');
    }

    /**
     * \return Returns html content for a simple Dialog Box.
     */
    private function buildDeleteEventOKWindow() {
        return $this->html_buillder->buildSimpleDialogueBox('dialog-deleteEventOK', 'Delete Event Successful', 'The selected event was deleted successfully.', 'ui-icon-check');
    }

    /**
     * \return Returns html content for confirmation modal window.
     */
    public function showDeleteEventConfirmModalWindow() {
        return $this->buildDeleteEventConfirmModalWindow();
    }

    /**
     * \return Returns html content for confirmation of deleting event modal window.
     */
    public function showDeleteEventOKWindow() {
        return $this->buildDeleteEventOKWindow();
    }

    /**
     * \param eventID is an interger
     * \return Returns html content for event.
     */
    public function showDetailedContentForEvent($eventId) {
        return $this->buildDetailedContentForEvent($eventId);
    }

    /**
     * \return Returns html content for current activities.
     */
    public function getCurrentEventList() {
        return $this->listAllActivitiesbyName();
    }

    /**
     * \return Returns html content for event Dialog.
     */
    public function showDetailedContentForEventDialog() {
        return $this->buildDetailedContentForEventDialog();
    }

}
?>
